package com.hxkj.api.impl;

import com.hxkj.api.PaymentClientApi;
import com.hxkj.domain.bo.PayChannelConstant;
import com.hxkj.domain.bo.PayParameter;
import com.hxkj.domain.bo.WithdrawParameter;
import com.hxkj.service.AliPayService;
import com.hxkj.service.KuaijiePayService;
import com.hxkj.service.ShunchangPayService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 支付控制器
 * <p>
 * Create by chenwei on 2018/6/26 16:04
 */
@RestController
@Slf4j
public class PaymentApiImpl implements PaymentClientApi {

    @Resource
    private AliPayService paymentService;

    @Resource
    private ShunchangPayService shunchangPayService;

    @Resource
    private KuaijiePayService kuaijiePayService;

    @Override
    public ResultDataStruct pay(@RequestBody PayParameter payParameter) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        try {
            // 支付宝
            if (StringUtil.equals(PayChannelConstant.ALIPAY, payParameter.getPayType())) {
                resultDataStruct.setData(paymentService.alipay(payParameter));
                // 顺畅支付
            } else if (StringUtil.equals(PayChannelConstant.SHUN_CHANG, payParameter.getPayType())) {
                resultDataStruct.setData(shunchangPayService.pay(payParameter));
                //
            } else if (StringUtil.equals(PayChannelConstant.KUAI_JIE, payParameter.getPayType())) {
                resultDataStruct.setData(kuaijiePayService.pay(payParameter));
            } else {
                // 默认支付方式为支付宝
                resultDataStruct.setData(paymentService.alipay(payParameter));
            }
            resultDataStruct.setMessage("成功");
            resultDataStruct.setCode(200);

        } catch (Exception e) {
            resultDataStruct.setMessage(e.getMessage());
            resultDataStruct.setCode(400);
        }
        return resultDataStruct;
    }

    @Override
    public ResultMessageStruct getTradeStatus(@RequestBody PayParameter payParameter) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            paymentService.getPayStatus(payParameter);
            resultMessageStruct.setCode(200);
            resultMessageStruct.setMessage("支付成功");
        } catch (CustomMessageException e) {
            resultMessageStruct.setCode(e.getCode());
            resultMessageStruct.setMessage(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            resultMessageStruct.setCode(400);
            resultMessageStruct.setMessage(e.getMessage());
        }
        return resultMessageStruct;
    }

    @Override
    public ResultMessageStruct withdraw(@RequestBody WithdrawParameter withdrawParameter) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {

            if(withdrawParameter.getPayType().equals("kuaijie")){
                kuaijiePayService.withdraw(withdrawParameter);
            }
            if(withdrawParameter.getPayType().equals("alipay")){
                paymentService.withdraw(withdrawParameter);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("申请人:【{}】订单:{}提现申请失败,失败详情:{}",
                    withdrawParameter.getAccountName(), withdrawParameter.getOrderId(), e.getMessage());
            resultMessageStruct.setMessage(e.getMessage());
            resultMessageStruct.setCode(400);
        }
        return resultMessageStruct;
    }


}
